<?php
function asplode($string) {
	$string = stripslashes($string);
	$string = preg_replace( "/=/", '&equals;', $string);
	$string = preg_replace( "/\'/", '&apos;', $string);
	$string = preg_replace( "/\"/", '&quot;', $string);
	return $string;
}

function rel_date($posted_date) {
	// thanks to maniacal rage, i converted this script for direct timestamp access
	// http://graveyard.maniacalrage.net/archives/2004/02/relativedatesusing/
	$relative_date = "";
	$in_seconds = $posted_date;
	    $diff = time()-$in_seconds;    $months = round($diff/2592000);    $diff -= $months*2419200;    $weeks = round($diff/604800);    $diff -= $weeks*604800;    $days = round($diff/86400);    $diff -= $days*86400;    $hours = round($diff/3600);    $diff -= $hours*3600;    $minutes = round($diff/60);    $diff -= $minutes*60;    $seconds = $diff;    if ($months>0) {
	    $relative_date .= ($relative_date?', ':'').$months.' month'.($months>1?'s':'');        	} elseif ($days>0) {	    // days and hours	    $relative_date .= ($relative_date?', ':'').$days.' day'.($days>1?'s':'');	} elseif ($hours>0) {	    // hours and minutes	    $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours>1?'s':'');	    $relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':''):'';	} elseif ($minutes>0) {	    // minutes only	    $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':'');	} else {	    // seconds only	    $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds>1?'s':'');    }    // show relative date and add proper verbiage    return $relative_date.' ago';
}

function array_write($array, $file) {
	/* Write an array to ini file  THIS IS NOT DONE*/
	$data = "";
	foreach($array as $key => $value) {
		$data .= "[" . $key . "]\n";		$data .= "date = \"" . $array[$key]['date'] . "\"\n";		$data .= "title = \"". $array[$key]['title'] . "\"\n";		$data .= "content = \"" . $array[$key]['content'] . "\"\n";
		$data .= "author = \"" . $array[$key]['author'] . "\"\n";		$data .= "\n";
	}
		$fp = fopen($file, "w");		fwrite ($fp, $data);		fclose ($fp);
}

function preview($id, $db) {
	show_one($id);
}

function list_posts($db) {
	$array = parse_ini_file($db, true);
	foreach($array as $key => $value) {
		echo '<tr>' . "\n\t";
		
		echo '<td>';
		echo '<input name="' . $key . '" type="checkbox" />';
		echo '</td>' . "\n\t";
				
		echo '<td style="text-align:left;">';
		echo '<a href="page.php?p=manage&view=' . $key . '">' . $value['title'] . '</a>';
		echo '</td>' . "\n\t";
								
		echo '<td>';
		echo rel_date($key);
		echo '</td>' . "\n";
		echo '</tr>' . "\n\n";
	}}
	
function add_post($title, $content, $date, $author, $db) {
	$original = file_get_contents($db);
	// get rid of those pesky equal signs, and apostrophes
	$content = asplode($content);
	$title = asplode($title);
	
	// make the category the timestamp
	$data = "[$date]\n";
	// our title
	$data .= "title = \"$title\"\n";
	// and our content
	$data .= "content = \"$content\"\n";
	// and our author
	$data .= "author = \"" . $author . "\"\n";
	$data .="\n";
	
	$data .= $original;
	// open our file
	$fp = fopen($db, "w");
	// write to the stream
	fwrite ($fp, $data);
	// close it up
	fclose ($fp);	
	echo $data;
}

function delete_post($id, $db) {	$content = parse_ini_file($db, true);
	if (array_key_exists($id, $content)){
		$old_title = $content[$id]['title'];
		$result = 'The post <strong>"' . $old_title . '"</strong>was deleted succesfully.';		
		unset($content[$id]);
		array_write($content, $db);
	}
	else { $result = 'The entry' . $id . ' does not exist.';}
	return ($result);}



?>